﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.IO.Compression;

namespace Marbles.Core.Filters
{
    public class DeflateResponseFilter : HttpResponseFilter
    {

        public override string ContentEncoding
        {
            get
            {
                return "deflate";
            }
        }

        private DeflateStream m_stream = null;

        public DeflateResponseFilter(Stream output)
            : base(output)
        {
            m_stream = new DeflateStream(output, CompressionMode.Compress);
        }


        public override void Write(byte[] buffer, int offset, int count)
        {
            if (!HeadersWritten) { WriteHeaders(); }
            m_stream.Write(buffer, offset, count);
        }

        public override void Close()
        {
            m_stream.Close();
        }

        public override void Flush()
        {
            m_stream.Flush();
        }
    }
}
